package com.itheima.web.controller;

import com.itheima.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-11 09:37
 * @description
 */

/*
    注意事项
 *  参数绑定注解
 *  @RequestParam
 *
 *  value="起名字"
 *
 *  required=true 默认值 强制要求前端必须给 不给参数就报错了
 *
 *  default:默认值!!!!!
 *
 *  参数绑定如果是对象类型的:
 *      它的javaBean属性名 和前端提交的名字必须一样
 *          ==>指的是setter/getter方法 去掉 get/set 前缀之后那个名字 驼峰转换掉
*/

@Controller
@RequestMapping("/user07")
public class demo7_参数绑定注解 {

    @RequestMapping("/login.do")
    public String login(Model model,
                        @RequestParam(value="username",required = false) String asd,
                        @RequestParam(value="password",required = false,defaultValue = "前端不传,默认值为123") String xyz
    ) throws ServletException, IOException {
        System.out.println("用户登录...");

        System.out.println("获取用户名:"+asd);
        System.out.println("获取用密码:"+xyz);

        //调用service  查询用户.......

        model.addAttribute("info","模拟的登录数据");

        return "user/success";
    }

    @RequestMapping("/register.do")
    public String register(Model model,User user) throws ServletException, IOException {
        System.out.println("用户注册...");

        System.out.println("封装完的结果:"+user);

        //调用service 保存用户....

        model.addAttribute("info","模拟的注册数据");

        return "user/success";

    }

    @RequestMapping("/logout.do")
    public String logout(Model model) throws ServletException, IOException {
        System.out.println("退出登录...");

        model.addAttribute("info","模拟的推出登录数据");

        return "user/success";

    }
}
